#include "RS400_all.h"
	unsigned int RS400_downloadSessionWithPdd(std::string path, int sessionNum,
																						polarLINUXio_general_settings &u, rs400_session &session)
{
	unsigned char buf[RS400_MAX_PACKET_SIZE];
	int size;
	//char filenamepdd[100],filenamehrm[100],filenamehrmfull[100];
	
	//check if the file already exist
	unsigned char query1[] = { RS400_CMD_GETSESSION, sessionNum, 0x00, 0x32 }; //just a small header
	RS400_sendbytes(query1, sizeof(query1));
	RS400_recvbytes(buf);
	RS400_parse_session_header(buf+4, session); //faster than downloading

	//search for pdd file
	rs400_pdd_content dayContent;

	std::string filenamepdd=path+buildPDDfilename(session.year,session.month,session.day);
	//printf("filename=%s\n",filenamepdd);
	int status = checkFileExistence(filenamepdd.c_str());
	//printf("\t \t \t status==%d\n",status);
	unsigned int starttime=session.start_hour*3600+session.start_minute*60+session.start_second;
	if (status==0) //pdd file exists for this day
	{
		_readPDD(filenamepdd.c_str(), dayContent);
		for (unsigned int j=0;j<dayContent.dayinfo.nbrExercises;j++) //check if this session exists in the pdd file
		{
			if (starttime==dayContent.exerciseinfo[j].startTime) //exercise exists
			{
				status=1;
				break;
			}
		}
	}
	//printf("status=%d\n",status);

	if (status==-1) //pdd file not found, generate day info
	{
		status=0;
		_initialize_pdd(dayContent,session.day,session.month,session.year);
	}

	if (status==0) //pdd file exists, but session does not exist
	{
		//Write hrm file
		std::cout<<"Session "<<sessionNum+1<<": downloading..."<<std::endl;
		size = RS400_get_session(sessionNum, buf);
		RS400_parse_session(buf, session);
		
		//generate hrm file name
		std::string filenamehrm=buildHRMfilename(session.year-2000,session.month,session.day,dayContent.dayinfo.nbrExercises+1);
		std::string filenamehrmfull=path+filenamehrm;

		//generate debug filename
		/*
		char filenameDEBUG[100];
		sprintf(filenameDEBUG,"%s",path);
		
		sprintf(filenameDEBUG+strlen(path),"%2d%02d%02d%02d.dbg",session.year-2000,
						session.month,session.day,toto.dayinfo.nbrExercises+1);

		saveBuffer(buf,size,filenameDEBUG);
		*/
		
		//patch sportzones values: these are not int the downloaded data!
		session.sportzone[5]=u.sportZvalues[5]; //100
		session.sportzone[4]=u.sportZvalues[4]; //90
		session.sportzone[3]=u.sportZvalues[3]; //80
		session.sportzone[2]=u.sportZvalues[2]; //70
		session.sportzone[1]=u.sportZvalues[1]; //60
		session.sportzone[0]=u.sportZvalues[0]; //50

		_saveHRM(filenamehrmfull.c_str(), session);

		// build stats
		int histogram[6];
		for (unsigned int ii=0;ii<6;ii++) histogram[ii]=0;
		float energy=0;
		for (unsigned int j=0;j<session.number_of_samples;j++)
		{
			if (session.sample[j].hr<=session.user_hr_max && session.sample[j].hr>=session.user_hr_rest)
			{
				float pc = 100.0*(float)session.sample[j].hr/session.user_hr_max;
				if (pc>u.sportZvalues[4]) histogram[5]+=1;
				else if (pc>u.sportZvalues[3]) histogram[4]+=1;
				else if (pc>u.sportZvalues[2]) histogram[3]+=1;
				else if (pc>u.sportZvalues[1]) histogram[2]+=1;
				else if (pc>u.sportZvalues[0]) histogram[1]+=1;
				else histogram[0]+=1;
				energy+=compute_Kcal((float)session.sample[j].hr/session.user_hr_max,session.user_weight,session.user_vo2_max)*session.recording_interval;
			}
		}

		//generate pdd data
		int interv = session.recording_interval;
		dayContent.dayinfo.nbrExercises+=1;
		dayContent.dayinfo.weight=100*session.user_weight; //pdd can take tenth, but not hrm
		dayContent.dayinfo.restingHR=session.user_hr_rest;
		
		rs400_pdd_exercise_info exInfo;
		exInfo.fileVersionDAY=101;
		exInfo.nbrInfoRows=1;
		exInfo.nbrNumRows=24;
		exInfo.nbrNumColumns=6;
		exInfo.nbrTextRows=12;
		exInfo.maxCharPerTextRow=512;

		exInfo.startTime=starttime;
		exInfo.totalTime=session.recording_interval*session.number_of_samples;
		exInfo.sport=1;
		exInfo.distanceOLD=0;
		exInfo.feeling=1;
		exInfo.recovery=0;
		exInfo.energy=energy;

		exInfo.totalExertion=0;
		exInfo.zone9Time=0; //(histogram[0]+histogram[1])*interv;
		exInfo.zone8Time=0; //(histogram[2]+histogram[3])*interv;
		exInfo.zone7Time=0; //(histogram[4]+histogram[5])*interv;
		exInfo.zone6Time=0; //(histogram[6]+histogram[7])*interv;
		exInfo.zone5Time=histogram[0]*interv;
		exInfo.zone4Time=histogram[1]*interv;
		exInfo.zone3Time=histogram[2]*interv;
		exInfo.zone2Time=histogram[3]*interv;
		exInfo.zone1Time=histogram[4]*interv;
		exInfo.zone0Time=histogram[5]*interv;

		exInfo.zone0Exertion=0;
		exInfo.zone1Exertion=0;
		exInfo.zone2Exertion=0;
		exInfo.zone3Exertion=0;
		exInfo.zone4Exertion=0;
		exInfo.zone5Exertion=0;
		exInfo.zone6Exertion=0;
		exInfo.zone7Exertion=0;
		exInfo.zone8Exertion=0;
		exInfo.zone9Exertion=0;

		exInfo.recordingRate=session.recording_interval;
		exInfo.HRaverage=session.hr_avg;
		exInfo.HRmax=session.hr_max;
		exInfo.speedAverage=0;
		exInfo.speedMax=0;
		exInfo.cadenceAverage=0;
		exInfo.cadenceMax=0;

		exInfo.toFinish=1; //TODO
		strcpy(exInfo.exoName,"myExo");
		strcpy(exInfo.exoNote,"myNote");
		strcpy(exInfo.exoHRMfile,filenamehrm.c_str());
		dayContent.exerciseinfo.push_back(exInfo);
		_savePDD(filenamepdd.c_str(), dayContent);
		std::cout<<"saved as "<<filenamehrm<<" and "<<filenamepdd<<std::endl;
	}
	return 0;
}
